package org.blogwu.learn.multithread.chapter3.demo21;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

public class Main {

    static volatile AtomicReference<Integer> atomicInteger = new AtomicReference();

    public static class AtomicThread extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                Integer ger = atomicInteger.get();
                atomicInteger.compareAndSet(atomicInteger.get(), atomicInteger.get() + 1);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Main.atomicInteger.set(new Integer(0));
        ExecutorService executorService = Executors.newFixedThreadPool(200);
        for (int i = 0; i < 200; i++) {
            executorService.submit(new AtomicThread());
        }
        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.HOURS);
        System.out.println(atomicInteger.get());
    }
}
